package com.green.service.manage.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.green.dao.manage.DataPermissionDao;
import com.green.dao.manage.OrgDao;
import com.green.model.manage.DataPermission;
import com.green.model.manage.Org;
import com.green.model.manage.Person;
import com.green.model.manage.Position;
import com.green.service.common.ServiceContext;
import com.green.service.common.impl.BaseServiceImpl;
import com.green.service.manage.DataPermissionService;



/**
 * 数据权限
 * 
 */
@Repository("dataPermissionService")
public class DataPermissionServiceImpl extends BaseServiceImpl implements DataPermissionService {
	@Autowired
	private DataPermissionDao dataPermissionDao;
	@Autowired
	private OrgDao orgDao;
	@Override
	public int insertDataPermission(DataPermission ct) {
		return dataPermissionDao.insertDataPermission(ct);
	}

	@Override
	public int updateDataPermission(DataPermission ct) {
		return dataPermissionDao.updateDataPermission(ct);
	}

	@Override
	public int deleteDataPermission(Map<String, Object> param) {
		return dataPermissionDao.deleteDataPermission(param);
	}

	@Override
	public DataPermission getDataPermissionById(String id) {
		return dataPermissionDao.getDataPermissionById(id);
	}

	@Override
	public List<DataPermission> getDataPermissionList(Map<String, Object> param) {
		return dataPermissionDao.getDataPermissionList(param);
	}

	@Override
	public String getDataPermissionWmConcat(Map<String, Object> param) {
		return dataPermissionDao.getDataPermissionWmConcat(param);
	}

	@Override
	public List<Org> getDataPermissionLoin(Map<String, Object> param) {
		List<Org> orgList =new ArrayList<Org>();
		List<DataPermission> dpList=dataPermissionDao.getDataPermissionList(param);
		Map<String, Object> map = new HashMap<String, Object>();
		for(DataPermission dp:dpList){			
			map.put("longNumber", dp.getOrg().getLongNumber());
			List<Org> o=orgDao.getOrgList(map);
			for(Org org:o){
				orgList.add(org);
			}
			
		}
		return orgList;
	}

	/**
	 * 保存 数据查看权限
	 * 
	 * @return
	 */
	@Override
	public int saveDataPermission(String personId,String positionId,String orgList) {
		//先删除  在添加
		Map<String, Object> param = new HashMap<String, Object>();
		//根据人员
		if(null!=personId&&!personId.equals(""))
		{
		param.put("personId", personId);
		this.deleteDataPermission(param);
		}
		//根据岗位
		if(null!=positionId&&!positionId.equals(""))
		{
		param.put("positionId", positionId);
		this.deleteDataPermission(param);
		}		
		
		int count = 0;
		if (null != orgList) {
			String[] idList = orgList.split(",");
			for (String obj : idList) {
				Org org=new Org();
				org.setId(obj);
				
				DataPermission dataPermission=new DataPermission();

				//人员关系
				if(null!=personId&&!personId.equals(""))
				{
				Person person =new Person();
				person.setId(personId);
				dataPermission.setPerson(person);
				}
				//岗位关系
				if(null!=positionId&&!positionId.equals(""))
				{
					Position position =new Position();
					position.setId(positionId);
				    dataPermission.setPosition(position);
				}
				dataPermission.setCreateBy(ServiceContext.getLoginPerson());
				dataPermission.setId(UUID.randomUUID().toString());
				dataPermission.setOrg(org);
				count=this.insertDataPermission(dataPermission);
			}
		}else{
			 count = 1;
		}
		return count;
	}
	
	
}
